<?php
class pos extends controller {
	public $kategoriak, $termekek, $lista, $osszeg;

	public function __construct($url){
		parent::__construct($url, true);
	}

	public function check(){}

	public function collect(){
		$this->kategoriak();
		$this->termekek();
	}

	public function run(){
		$this->vasarlas();
		$this->visszavonas();
		$this->listatorlese();
		$this->listahozadas();
		$this->lista();
	}

	private function kategoriak(){
		$sql = "SELECT id, megnevezes FROM kategoria WHERE felhasznalo = ".$this->usr[0];
		$this->kategoriak = $this->db->fetch($sql);
	}

	private function termekek(){
		if(!empty($this->url[1])){
			$sql = "SELECT id, megnevezes FROM termek WHERE kategoria = ".$this->url[1];
			$this->termekek = $this->db->fetch($sql);
		}
		else{
			$this->termekek = null;
		}
	}

	private function vasarlas(){
		if(isset($_POST["vasarlas"])){
			if(!empty($_SESSION["kosar"])){
				foreach($_SESSION["kosar"] as $elem){
					$sql = "INSERT INTO eladas(termek, mennyiseg, felhasznalo, datum) VALUES(".$elem[0].", ".$elem[1].", ".$this->usr[0].", '".date("Y-m-d")."')";
					$this->db->query($sql);
					$this->alapanyagokbol($elem[0]);
				}
				unset($_SESSION["kosar"]);
				$this->msg = "{".$sql."}";
				//$this->redirect("pos");
			}
		}
	}

	private function visszavonas(){
		if(isset($_POST["visszavonas"])){
			if(0 < count($_SESSION["kosar"])){
				unset($_SESSION["kosar"][count($_SESSION["kosar"])-1]);
				$this->redirect("pos");
			}
		}
	}

	private function listatorlese(){
		if(isset($_POST["listatorlese"])){
			unset($_SESSION["kosar"]);
			$this->redirect("pos");
		}
	}

	private function listahozadas(){
		if(isset($_POST["listahozadas"])){
			if(!empty($_POST["termek"]) && !empty($_POST["mennyiseg"])){
				$_SESSION["kosar"][] = array($_POST["termek"], $_POST["mennyiseg"]);
				$this->redirect("pos");
			}
			else{
				$this->msg = "Üresen hagyott mező!";
			}
		}
	}

	private function alapanyagokbol($termek){
		$sql = "SELECT alapanyag, mennyiseg FROM recept WHERE termek = ".$termek;
		$recept = $this->db->fetch($sql);
		if($recept != null){
			foreach($recept as $elem){
				$sql = "UPDATE alapanyag SET mennyiseg = mennyiseg - ".$elem[1]." WHERE id = ".$elem[0];
				$this->db->query($sql);
			}
		}
	}

	private function lista(){
		$this->osszeg = 0;
		if(isset($_SESSION["kosar"]) && 0 < count($_SESSION["kosar"])){
			foreach($_SESSION["kosar"] as $elem){
				$sql = "SELECT termek.megnevezes, kategoria.megnevezes, termek.ar FROM termek, kategoria WHERE termek.kategoria = kategoria.id AND termek.id = ".$elem[0];
				$tmp = $this->db->fetch($sql);
				$this->lista[] = array($tmp[0][0], $tmp[0][1], $elem[1], $elem[1]*$tmp[0][2]);
				$this->osszeg += $elem[1]*$tmp[0][2];
			}
		}
		else{
			$this->kosar = null;
		}
	}
}
?>